package test;

import common.Utils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;

public class RegisterPage extends Utils {
    private static String URL = "http://113.44.92.31:65533/sign-up.html";
    public RegisterPage() {
        super(URL);
    }

    /**
     * 检查注册页面的元素
     */
    public void checkRegisterElements() {
        driver.findElement(By.cssSelector("#signUpForm > div > h2"));
        driver.findElement(By.cssSelector("#signUpForm > div > div:nth-child(2) > label"));
        driver.findElement(By.cssSelector("#username"));
        driver.findElement(By.cssSelector("#signUpForm > div > div:nth-child(3) > label"));
        driver.findElement(By.cssSelector("#nickname"));
        driver.findElement(By.cssSelector("#signUpForm > div > div:nth-child(4) > label"));
        driver.findElement(By.cssSelector("#password"));
        driver.findElement(By.cssSelector("#signUpForm > div > div:nth-child(5) > label"));
        driver.findElement(By.cssSelector("#passwordRepeat"));
        driver.findElement(By.cssSelector("#signUpForm > div > div:nth-child(6) > label"));
        driver.findElement(By.cssSelector("#email"));
        driver.findElement(By.cssSelector("#signUpForm > div > div:nth-child(7) > label"));
        driver.findElement(By.cssSelector("#code"));
        driver.findElement(By.cssSelector("#policy"));
        driver.findElement(By.cssSelector("#signUpForm > div > div:nth-child(8) > label > span > a"));
        driver.findElement(By.cssSelector("#submit"));
    }

    /**
     * 注册成功
     */
    public void registerSuccess() throws InterruptedException {
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#nickname")).clear();
        driver.findElement(By.cssSelector("#password")).clear();
        driver.findElement(By.cssSelector("#passwordRepeat")).clear();
        driver.findElement(By.cssSelector("#email")).clear();
        driver.findElement(By.cssSelector("#code")).clear();
        driver.findElement(By.cssSelector("#username")).sendKeys("lisi");
        driver.findElement(By.cssSelector("#nickname")).sendKeys("lisi");
        driver.findElement(By.cssSelector("#password")).sendKeys("0901");
        driver.findElement(By.cssSelector("#passwordRepeat")).sendKeys("0901");
        driver.findElement(By.cssSelector("#email")).sendKeys("2802031108@qq.com");
        driver.findElement(By.cssSelector("#sendCode")).click();
        Thread.sleep(15000);//手动输入验证码
        driver.findElement(By.cssSelector("#policy")).click();
        driver.findElement(By.cssSelector("#submit")).click();
        //验证是否跳转成功
        Thread.sleep(1000);
        assert  driver.getCurrentUrl().equals("http://113.44.92.31:65533/sign-in.html");
    }

    /**
     * 注册失败- 邮箱已被注册
     */
    public void registerFail() throws InterruptedException {
        driver.get(URL);
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#nickname")).clear();
        driver.findElement(By.cssSelector("#password")).clear();
        driver.findElement(By.cssSelector("#passwordRepeat")).clear();
        driver.findElement(By.cssSelector("#email")).clear();
        driver.findElement(By.cssSelector("#code")).clear();

        driver.findElement(By.cssSelector("#username")).sendKeys("chen");
        driver.findElement(By.cssSelector("#nickname")).sendKeys("chen");
        driver.findElement(By.cssSelector("#password")).sendKeys("jun");
        driver.findElement(By.cssSelector("#passwordRepeat")).sendKeys("jun");
        driver.findElement(By.cssSelector("#email")).sendKeys("2802031108@qq.com");
//        driver.findElement(By.cssSelector("#sendCode")).click();
        Thread.sleep(15000);//手动输入验证码
        driver.findElement(By.cssSelector("#policy")).click();
        driver.findElement(By.cssSelector("#submit")).click();
        element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")));
        if (element.isDisplayed()) {
            String text = element.getText();
            String[] lines = text.split("\n");
            String message = lines[2];
            assert message.equals("该邮箱已被注册");
        } else {
            System.out.println("Element is not visible.");
        }
    }
}
